package fr.enseirb.mycub.json.impl;

import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import fr.enseirb.mycub.PrefManager;
import fr.enseirb.mycub.R;
import fr.enseirb.mycub.activity.AbstractPreferencesActivity;
import fr.enseirb.mycub.db.DBAdapter;
import fr.enseirb.mycub.db.ParkingDBHelper;
import fr.enseirb.mycub.db.structure.ParkingDAO;
import fr.enseirb.mycub.json.JSONHandler;
import fr.enseirb.mycub.task.GetJSONAsyncTask;
import fr.enseirb.mycub.task.GetJSONMockAsyncTask;
import fr.enseirb.mycub.utils.Constants;
import fr.enseirb.mycub.utils.ParserUtils;
import fr.enseirb.mycub.utils.SeekBarPreference;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.util.Log;

public class ParkingFullJSONHandler extends JSONHandler{

	public static final String PARKINGS = "parkings";

	public ParkingFullJSONHandler(Context context){
		super(context);
		this.preference_key_last_update_date = "ParkingJSONHandler_last_update_date";
		this.preference_key_interval_adress = "ParkingJSONHandler_interval_adress";
		this.preference_key_interval_between_update = "ParkingJSONHandler_interval_between_update";

		this.preference_key_interval_between_update_default = 60000000;
	}

	@Override
	public boolean updateLocalDatabase(String json) {
		Log.i("ParkingJSONFull", "update local db");
		boolean success = false;
		DBAdapter dbAdapter = DBAdapter.getInstance(context);
		try {
			//TODO MOCK
			//json = getMock();
			Gson gson = new GsonBuilder().create();
			JsonResponse dao = gson.fromJson(json, JsonResponse.class);
			Log.i("ParkingFull","json response :"+json);
			if (dao != null) {
				JsonResponseTranformed dbDAO = new JsonResponseTranformed(dao);
				Log.i("ParkingFull","transformation :"+dbDAO.toString());
				ParkingDBHelper.cleanAndInsertParkings(dbAdapter.getDB(), dbDAO.stationsDAO);
				success = true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return success;
	}

	@Override
	protected void standardUpdate() {
		String baseAddress = Constants.LOCAL_SERVER_URL+"/freeparking/";
		String address = baseAddress+44+"/"+0+"/"+1000000000;
		if(PrefManager.mgr.getBoolean(
				AbstractPreferencesActivity.ENABLE_DISTANCE_FILTER,
				false) && longitude != 1337 && latitude != 1337){
			int radius = PrefManager.mgr.getInt(AbstractPreferencesActivity.DISTANCE_FILTER,
					SeekBarPreference.DEFAULT_DISTANCE);
			address = baseAddress+longitude+"/"+latitude+"/"+radius;
			
		}
		Log.i("JSON Handler", address);
		new GetJSONAsyncTask(this, address).execute();
	}

	public String getMock(){
		try {
			return	ParserUtils.getString(context.getResources().openRawResource(R.raw.freeparking));
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private class JsonResponse{
		ArrayList<ParkingJsonDAO> parkings;
	}


	private class JsonResponseTranformed{
		ArrayList<ParkingDAO> stationsDAO;

		public JsonResponseTranformed(JsonResponse dao){
			
			stationsDAO = ParkingJsonDAO.getDAOList(dao.parkings);

		}

	}

	static class ParkingJsonDAO{
		@SerializedName(ID)
		protected int mId;
		@SerializedName(NAME)
		protected String mName;
		ArrayList<Double> location;
		private String address;
		protected boolean mIsFavorite;
		@SerializedName(PLACES)
		private int places;
		
		public static final String ID = "id";
		public static final String NAME = "name";
		public static final String OPEN = "open";	
		public static final String LATITUDE = "latitude";
		public static final String LONGITUDE = "longitude";
		public static final String PLACES = "places";

		public ParkingDAO getDAO(){
			ParkingDAO item = new ParkingDAO();
			item.setId(mId);
			item.setmLatitude(location.get(1));
			item.setmLongitude(location.get(0));
			item.setName(mName);
			//gestion du statut pas encore active
			item.setOpen(true);
			//pas d'adresse :(
			item.setAddress("");
			item.setPlaces(getPlaces());
			return item;
		}

		public static ArrayList<ParkingDAO> getDAOList(ArrayList<ParkingJsonDAO> list){
			ArrayList<ParkingDAO> finalList = new ArrayList<ParkingDAO>();
			int size = list.size();
			for(int i = 0; i < size; i++){
				finalList.add(list.get(i).getDAO());
			}
			return finalList;
		}

		public String getName() {
			return mName;
		}

		public void setName(String name) {
			mName = name;
		}

		public void setId(int id) {
			this.mId = id;
		}

		public int getId() {
			return this.mId;
		}

		public boolean isFavorite() {
			return mIsFavorite;
		}

		public void setFavorite(boolean isFavorite) {
			mIsFavorite = isFavorite;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}

		public int getPlaces() {
			return places;
		}

		public void setPlaces(int places) {
			this.places = places;
		}



	}

}
